package com.example.spring.boot.rabbitmq.controller;

import com.example.spring.boot.rabbitmq.constant.DirectExchangeConstant;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * <p>
 * DirectExchangeController
 * </p>
 *
 * @author weixin
 * @date 2023/3/28 14:42
 */
@Slf4j
@RestController
@RequestMapping("/direct/")
public class DirectExchangeController {

    private final RabbitTemplate rabbitTemplate;

    public DirectExchangeController(RabbitTemplate rabbitTemplate) {
        this.rabbitTemplate = rabbitTemplate;
    }

    @GetMapping("sendDirect")
    public String sendDirect() {
        rabbitTemplate.convertAndSend(DirectExchangeConstant.DIRECT_EXCHANGE,
                DirectExchangeConstant.DIRECT_ROUTING_KEY, "发送一条测试消息：direct");
        log.info("发送直连消息时间：{}", LocalDateTime.now().format(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        return "direct消息发送成功！！";
    }

}
